//
//  ImageView.m
//  GeoPhoto2
//
//  Created by walkerbe on 2/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ImageView.h"
#import "ViewController.h"
#import "InfoDelegateProtocol.h"
#import "ImageInfoDataObject.h"

/*
@interface ImageView()
@property (strong, nonatomic) UINavigationController *masterNavigationController;
- (void)configureView;
@end
*/

@implementation ImageView
@synthesize imageView;
@synthesize imageView2;
@synthesize scrollView;
@synthesize minScale;
@synthesize contentSize;
@synthesize images;
@synthesize first;
@synthesize second;
@synthesize third;
@synthesize fourth;
@synthesize fifth;


- (ImageInfoDataObject *) theInfoDataObject;
{
    id<InfoDelegateProtocol> theDelegate = (id<InfoDelegateProtocol>) [UIApplication sharedApplication].delegate;
    ImageInfoDataObject *theDataObject;
    theDataObject = (ImageInfoDataObject*) theDelegate.theInfoDataObject;
    return theDataObject;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    [imageView removeFromSuperview];
    [imageView2 removeFromSuperview];
    self.images = nil;
    UIAlertView *memWarning = [[UIAlertView alloc] initWithTitle:@"Memory warning" message:@"App using too much memory, try using smaller or fewer images." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [memWarning show];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    if (first) return imageView;
    else if (second) return imageView2;
    else if (third) return imageView;
    else if (fourth) return imageView2;
    else return imageView;
    

}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    ImageInfoDataObject *theDataObject = [self theInfoDataObject];
    self.images = theDataObject.theInfo;
    NSInteger len = [self.images count];
    
    
    if (len == 0) {
        UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No images found. Try uploading again." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [theAlert show];
    }
    else if (len == 1) {
        //imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:[self.images objectAtIndex:0]]];
        imageView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
    }
    else if (len <=5){
        //imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:[self.images objectAtIndex:0]]];
        imageView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
        imageView2 = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
        //imageView2 = [[UIImageView alloc] initWithImage: [UIImage imageNamed:[self.images objectAtIndex:1]]];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Too many images selected" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [alert show];
    }
    
    
    self.first = TRUE;
    self.second = FALSE;
    self.third = FALSE;
    self.fourth = FALSE;
    self.fifth = FALSE;
    
    //scrollView.frame = self.view.frame;
    //scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    //scrollView.maximumZoomScale = 100.0;
    //scrollView.minimumZoomScale =0.05;
    //scrollView.clipsToBounds = YES;
    //CGRect tempframe = imageView.frame;
    //tempframe.size.width = scrollView.frame.size.width;
    //tempframe.size.height = scrollView.frame.size.height;
    //imageView.frame  = tempframe;
    
    //self.minScale = [scrollView frame].size.width  / [imageView frame].size.width;
    self.minScale = [scrollView frame].size.width  / [imageView frame].size.width;
    [scrollView setMinimumZoomScale:self.minScale];
    [scrollView setZoomScale:self.minScale];
    self.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    //self.minScale = CGSizeMake(<#CGFloat width#>, <#CGFloat height#>)
    NSLog(@"%f    %f", imageView.frame.size.width, imageView.frame.size.height);
    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    [scrollView setMaximumZoomScale:100];
    [scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    scrollView.clipsToBounds = YES;
    [scrollView addSubview:imageView];
    
    /*
    scrollView.frame = self.view.frame;
    //scrollView.contentSize = CGSizeMake([UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]].size.width, [UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]].size.height);
    //scrollView.contentSize = CGSizeMake(786, 916);
    scrollView.maximumZoomScale = 100.0;
    scrollView.minimumZoomScale =0.15;
    scrollView.clipsToBounds = YES;
    //CGRect tempframe = CGRectMake(0, 0, 786, 916);
    //tempframe.size = self.imageSize;
    //imageView.frame  = tempframe;
    [imageView sizeToFit];
    [scrollView addSubview:imageView];
     */


}

-(IBAction)doubleTap:(id)sender{
    NSLog(@"DOUBLE TAP: %f", scrollView.zoomScale);
    //[scrollView setZoomScale:self.minScale];
    [scrollView setContentSize:self.contentSize];
    [scrollView setZoomScale:self.minScale];
    NSLog(@"again: %f", scrollView.zoomScale);
    
}

-(IBAction)toggle:(id)sender{
    
    NSInteger theLength = [self.images count];
    NSLog(@"toggle: %f", scrollView.zoomScale);
    
    if (theLength == 0) {
    }
    else if (theLength == 1) {
        [scrollView addSubview:imageView];

    }
    
    else if (theLength == 2) {
        if (first ){
            //CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            //scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            //scrollView.maximumZoomScale = 100.0;
            //scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            //scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            NSLog(@"%f  %f", imageView.frame.size.width, imageView.frame.size.height);
            NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
            first = FALSE;
            second = TRUE;
        }else{
            NSLog(@"%f  %f", imageView.frame.size.width, imageView.frame.size.height);
            NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
           //CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            //scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            //scrollView.maximumZoomScale = 100.0;
            //scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            //scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
        }
         
    }
    
    else if (theLength == 3) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
        }
        
    }
    
    else if (theLength == 4) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;
            
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;
            
        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:3]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;
            
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            
        }
        
    }
    
    else if (theLength == 5) {
        if (first ){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:1]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:1]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = TRUE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (second){
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:2]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:2]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = TRUE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        else if (third) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView2 setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:3]]];
            //[imageView2 setImage:[UIImage imageNamed:[self.images objectAtIndex:3]]];
            CGRect frame = imageView2.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView2.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView2.frame.size.width, imageView2.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView2];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = TRUE;
            fifth = FALSE;
            
        }
        else if (fourth) {
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView2.frame.size;
            [imageView2 removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:4]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:4]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = FALSE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = TRUE;
            
        }
        else{
            CGPoint offSet = scrollView.contentOffset;
            CGSize tempsize = imageView.frame.size;
            [imageView removeFromSuperview];
            [imageView setImage:[UIImage imageWithContentsOfFile:[self.images objectAtIndex:0]]];
            //[imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:0]]];
            CGRect frame = imageView.frame;
            frame.size.width  = tempsize.width;
            frame.size.height = tempsize.height;
            imageView.frame = frame;
            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            scrollView.maximumZoomScale = 100.0;
            scrollView.minimumZoomScale =0.25;
            scrollView.clipsToBounds = YES;
            scrollView.contentOffset = offSet;
            [scrollView addSubview:imageView];
            first = TRUE;
            second = FALSE;
            third = FALSE;
            fourth = FALSE;
            fifth = FALSE;
            
        }
        
    }
    
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

@end
